﻿using System;
using System.Linq;
namespace e2
{
    public class Order
    {
        public int orderNum;
        public string product;
        public string customer;
        public double price;

        public override bool Equals(Object obj)
        {
            if (obj is Order)
            {
                Order m = (Order)obj;
                if (m.orderNum == this.orderNum && m.customer == this.customer && m.product == this.product && m.price == this.price)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            return base.Equals(obj);
        }
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
        public override string ToString()
        {
            return "订单号:" + this.orderNum + " 产品:" + this.product + " 顾客姓名:" + this.customer + " 订单金额:" + this.price;
        }
        public Order() { }
        public Order(int on, string p, string c, double pri)
        {
            this.orderNum = on;
            this.product = p;
            this.customer = c;
            this.price = pri;
        }
        public void setOrderNum(int on)
        {
            this.orderNum = on;
        }
        public void setProduct(string p)
        {
            this.product = p;
        }
        public void setCustomer(string c)
        {
            this.customer = c;
        }
        public void setPrice(double pri)
        {
            this.price = pri;
        }
        public int getOrderNum()
        {
            return this.orderNum;
        }
        public string getProduct()
        {
            return this.product;
        }
        public string getCustomer()
        {
            return this.customer;
        }
        public double getPrice()
        {
            return this.price;
        }
    }
    public class OrderService
    {
        public static Order[] orderList;
        public static int length;

        public static void ordering()
        {//排序算法
            Order tmp;
            for (int j = length - 1; j > 0; j--)
            {
                for (int i = 0; i < j; i++)
                {
                    if (orderList[i].getOrderNum() > orderList[i + 1].getOrderNum())
                    {
                        /*tmp = orderList[i].getOrderNum();
                        orderList[i].setOrderNum(orderList[i + 1].getOrderNum());
                        orderList[i + 1].setOrderNum(tmp);*/
                        tmp = orderList[i];
                        orderList[i] = orderList[i + 1];
                        orderList[i + 1] = tmp;
                    }
                }
            }
        }
        public OrderService()
        {
            orderList = new Order[100];
            length = 0;
        }
        public static void addOrder(int num,string product,string customer,double price)
        {
            /*Console.WriteLine("请输入订单号:");
            string num_s = Console.ReadLine();
            int num = Convert.ToInt32(num_s);
            Console.WriteLine("请输入产品名:");
            string productName = Console.ReadLine();
            Console.WriteLine("请输入顾客名:");
            string customerName = Console.ReadLine();
            Console.WriteLine("请输入订单金额:");
            string pri_s = Console.ReadLine();
            double pri = Convert.ToDouble(pri_s);*/
            //Order o = new Order(num, productName, customerName, pri);
            var allOrders = from item in orderList where item != null select item;
            allOrders = allOrders.Take(length);
            bool okToAdd = true;
            foreach (var item in allOrders)
            {
                /*if (item.Equals(o))
                {
                    o = null;
                    okToAdd = false;
                }*/
                if (item != null)
                {
                    if (item.getOrderNum() == num | (item.getProduct() == product && item.getCustomer() == customer && item.getPrice() == price))
                    {//保证每个订单的明细不重复
                        okToAdd = false;
                    }
                }

            }
            if (okToAdd)
            {
                Order o = new Order(num, product, customer, price);
                orderList[length] = o;
                length++;
                Console.WriteLine("添加成功");
               
            }
            else
            {
                Console.WriteLine("添加失败，已有相同订单");
               
            }

        }
        public static void deleteOrder(int num)
        {
            /*Console.WriteLine("请输入需要删除订单的订单号:");
            string num_s = Console.ReadLine();
            int num = Convert.ToInt32(num_s);*/
            bool find = false;
            var allOrders = from item in orderList where item != null select item;
            allOrders = allOrders.Take(length);
            int n = 0;
            foreach (var item in allOrders)
            {
                n++;
                if (item.getOrderNum() == num)
                {
                    find = true;
                    //int j = n;
                    while (n < length)
                    {
                        orderList[n - 1] = orderList[n];
                        n++;
                    }
                    length--;
                    Console.WriteLine("删除成功");
                }
            }
            if (!find)
            {
                Console.WriteLine("无此订单");
            }
        }
        public static void updateOrder(int num,string new_product,string new_customer,double new_price)
        {
            /*Console.WriteLine("请输入需要更改订单的订单号:");
            string num_s = Console.ReadLine();
            int num = Convert.ToInt32(num_s);*/
            bool find = false;
            var allOrders = from item in orderList where item != null select item;
            allOrders = allOrders.Take(length);
            int n = 0;
            foreach (var item in allOrders)
            {
                if (item.getOrderNum() == num)
                {
                    bool okToUpdate = true;
                    /*Console.WriteLine("请输入更改后的产品:");
                    string new_product = Console.ReadLine();
                    Console.WriteLine("请输入更改后的顾客:");
                    string new_customer = Console.ReadLine();
                    Console.WriteLine("请输入更改后的金额:");
                    string new_price_s = Console.ReadLine();
                    double new_price = Convert.ToDouble(new_price_s);
                    */
                    var allOrders2 = from item2 in orderList where item2 != null select item2;
                    allOrders2 = allOrders2.Take(length);

                    foreach (var item2 in allOrders2)
                    {
                        if (item.getProduct() == new_product && item.getCustomer() == new_customer && item.getPrice() == new_price)
                        {
                            okToUpdate = false;
                        }
                    }
                    if (okToUpdate)
                    {
                        orderList[n].setProduct(new_product);
                        orderList[n].setCustomer(new_customer);
                        orderList[n].setPrice(new_price);
                        find = true;
                        Console.WriteLine("修改成功！");
                    }
                    else
                    {
                        Console.WriteLine("修改失败，已有相同订单");
                    }


                }
                n++;
            }
            if (!find)
            {
                Console.WriteLine("无此订单");
            }
        }

        public static void selectOrder()
        {
            Console.WriteLine("请选择查询方式：");
            Console.WriteLine("1.订单号");
            Console.WriteLine("2.产品");
            Console.WriteLine("3.顾客");
            Console.WriteLine("4.金额");
            string choice = Console.ReadLine();
            if (choice == "1")
            {
                Console.WriteLine("请输入订单号:");
                string num_s = Console.ReadLine();
                int num = Convert.ToInt32(num_s);
                bool find = false;
                var allOrders = from item in orderList where item != null && item.getOrderNum() == num select item;
                allOrders = allOrders.Take(length);
                foreach (var item in allOrders)
                {
                    Console.WriteLine(item.ToString());
                    find = true;
                }
                if (!find)
                {
                    Console.WriteLine("无订单");
                }
            }
            else if (choice == "2")
            {
                Console.WriteLine("请输入产品名:");
                string productName = Console.ReadLine();
                bool find = false;
                var allOrders = from item in orderList where item != null && item.getProduct() == productName select item;
                allOrders = allOrders.Take(length);
                foreach (var item in allOrders)
                {
                    Console.WriteLine(item.ToString());
                    find = true;
                }
                if (!find)
                {
                    Console.WriteLine("无订单");
                }
            }
            else if (choice == "3")
            {
                Console.WriteLine("请输入顾客名：");
                string customerName = Console.ReadLine();
                bool find = false;
                var allOrders = from item in orderList where item != null && item.getCustomer() == customerName select item;
                allOrders = allOrders.Take(length);
                foreach (var item in allOrders)
                {
                    Console.WriteLine(item.ToString());
                    find = true;
                }
                if (!find)
                {
                    Console.WriteLine("无订单");
                }
            }
            else if (choice == "4")
            {
                Console.WriteLine("请输入订单金额:");
                string pri_s = Console.ReadLine();
                double pri = Convert.ToDouble(pri_s);
                bool find = false;
                var allOrders = from item in orderList where item != null && item.getPrice() == pri select item;
                allOrders = allOrders.Take(length);
                foreach (var item in allOrders)
                {
                    Console.WriteLine(item.ToString());
                    find = true;
                }
                if (!find)
                {
                    Console.WriteLine("无订单");
                }
            }
            else
            {
                Console.WriteLine("输入错误");
            }

        }

    }
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}
